// pages/chengZhangKa/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '',
    imgPath: '/images',
    //可使用的卡
    usableCards: [
      // {
      //   id: 1,
      //   status: '去激活',
      //   name: '',
      //   image: '',
      //   month: 1,
      //   endDate: '2020-5-8'
      // },
      // {
      //   id: 2,
      //   status: '去激活',
      //   name: '',
      //   image: '',
      //   month: 3
      // }, {
      //   id: 3,
      //   status: '去激活',
      //   name: '',
      //   image: '',
      //   month: 1
      // },
    ],
    //不可使用的卡
    unusableCards: [{
      id: 4,
      status: '已使用',
      name: '',
      image: '',
      month: 1
    }, {
      id: 5,
      status: '已过期',
      name: '',
      image: '',
      month: 3
    }],
    usableClass: 'headSelected',
    unusableClass: 'headNotSelected',
    //选择的tab:usable,notUsable
    selectedTab: 'usable',
    //显示使用兑换码
    showShiYong: false,
    //兑换码
    duiHuanMa: '',
    // 激活
    jihuo_status:false,
    cardId:'',
    refresh:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    let refresh = options.refresh;
    const util = require("../../utils/util")
    //图片路径
    this.setData({
      imgPath: util.getImgPath(),
      refresh
    })

    
  },
  saoma(){
    let that = this;
    wx.scanCode({
      success (res) {
        const api = require("../../api/api");
        api.card.cardExchange({
          card_sn:res.result,
          token:wx.getStorageSync('token'),
          success(res){
              if(res.data.code == 0){
                wx.showToast({
                    title: '兑换成功',
                    icon:'none',
                    duration: 5000,
                  })
                  setTimeout(function(){
                    wx.navigateTo({
                     // url: '/pages/chengZhangKa/index',
                     url: '/pages/index/index?query=chengzhangka',
                    })  
                  },1500);
               
              }else{
                  wx.showToast({
                    title: res.data.msg,
                     icon:'none',
                     duration: 5000,
                  })
              }
          }
        })

      //  console.log(res)
      }
    })
  },
  /**
   * 从接口获取卡数据
   */
  fetchData(){
    const that=this;
     console.log('刷新了一次');
    // return;
    this.setData({
      usableCards: [],
      unusableCards: []
    })
    const api = require("../../api/api")
    api.card.cardList({
      page: '1',
      page_size: '100',
      success(res) {
        // console.log("cardList,res", res)
        const rows = res.data.data.rows;
        // console.log("rows", rows)
        /**
         * {
        id: 1,
        status: '去激活',
        month: 1,
        endDate: '2020-5-8'
      }
         */

        for (const row of rows) {
          // console.log("row", row)
          let status = "去激活";
          if (row.active_status != 0) {
            status = "已使用"
          }
          if (row.end_time != null) {
            let end_time = new Date(row.end_time);
            let now = new Date();
            if (now > end_time) {
              status = "已过期"
            }
          }
          if (status == "去激活") {
            that.data.usableCards.push({
              id: row.member_card_id,
              name: row.name,
              image: row.image,
              status: status,
              month: row.valid_mouth,
              endDate: row.expired_at
            })
          } else {
            that.data.unusableCards.push({
              id: row.member_card_id,
              name: row.name,
              image: row.image,
              status: status,
              month: row.valid_mouth,
              endDate: row.expired_at
            })
          }
        }
        that.setData({
          usableCards: that.data.usableCards,
          unusableCards: that.data.unusableCards
        })

        // console.log("that.data",that.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //成长卡
    this.fetchData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onUsableTap(e) {
    this.setData({
      selectedTab: 'usable',
      usableClass: 'headSelected',
      unusableClass: 'headNotSelected'
    })
    // console.log("this.data",this.data)
  },
  onUnusabeleTap(e) {
    this.setData({
      selectedTab: 'notUsable',
      usableClass: 'headNotSelected',
      unusableClass: 'headSelected'
    })
    // console.log("this.data",this.data)
  },
  showDaiHuan() {
    this.setData({
      showShiYong: true
    })
  },
  onSyClose() {
    this.setData({
      showShiYong: false
    })
  },
  bindKeyInput(e) {

  },
  queRenShiYong(e) {
    let that = this;
    wx.showModal({
        title: '',
        content: '是否要兑换成长卡吗？',
        success:res=>{
         if (res.confirm) {
          const api = require("../../api/api");
          api.card.cardExchange({
            card_sn:that.data.duiHuanMa,
            success(res){
                if(res.data.code == 0){
                  wx.showToast({
                      title: '兑换成功',
                      icon:'none'
                    })
                 that.data.duiHuanMa = '';
                 that.onSyClose();
              
               if(that.data.refresh){
                  that.fetchData(); 
               }else{
                wx.navigateTo({
                  url: '/pages/index/index?query=chengzhangka',
                })
               }
                }else{
                    wx.showToast({
                      title: res.data.msg,
                       icon:'none'
                    })
                }
            }
          })
         } else if (res.cancel) {
          console.log('用户点击取消----')
         }
        }
       })
  },
  close_jihuo(){
     this.setData({
      jihuo_status:false
     });
  },
  new_jihuo(){
    this.setData({
      jihuo_status:true
     });
  },
  givefriends(e){
    wx.showToast({
      title: '敬请期待',
      icon:'none'
    })
  },
  jiHuo(e) {
    this.setData({
      jihuo_status:true
     });
    if(e.currentTarget.dataset.on == "onjihuo"){
      let that = this;
      let cardId = that.data.cardId;
    //  console.log('cardIdcardId',cardId);
    //  return;
      const api = require("../../api/api")
      api.card.activeCard({
        member_card_id: cardId,
        success(res) {
          // console.log("activeCard", res);
          if (res.data.code == 0) {
            wx.showToast({
              title: '已成功激活卡',
            })
            //刷新卡数据
            //that.fetchData();
            // 更新状态  要成为会员才有个人中心，不需要更新状态了
            api.user.home({
              success(res) {
                //更新home到缓存
                wx.setStorageSync('home', res.data.data);
                const util = require("../../utils/util");
                util.setBeforePlanned(true,res.data.data.is_member);
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              }
            })
           // let is_member = wx.getStorageSync('home').is_member;
            // wx.redirectTo({
            //   url: '/pages/index/index?query=chengzhangka',
            // })
          }else{
            wx.showToast({
              title: res.data.msg,
            })
          }
        }
  
      })
    }else{
      let cardId = e.detail.cardId;
      this.setData({
        cardId
      });
    }

  },
  onTabbarChange(event) {
    // console.log("daoQi,onTabbarChange,event", event)
    this.setData({
      active: event.detail
    })
  },
})